<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>温度传感器</title>
    <link rel="shortcut icon" href="favicon.ico"> <link href="/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="/css/font-awesome.css?v=4.4.0" rel="stylesheet">

    <link href="/css/animate.css" rel="stylesheet">
    <link href="/css/style.css?v=4.1.0" rel="stylesheet">

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>折线图</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="dropdown-toggle" data-toggle="dropdown" href="graph_flot.html#">
                            <i class="fa fa-wrench"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    <div class="echarts" style="height: 450px;" id="echarts-bar-chart"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- 全局js -->
<script src="/js/jquery.min.js?v=2.1.4"></script>
<script src="/js/bootstrap.min.js?v=3.3.6"></script>



<!-- ECharts -->
<script src="/js/plugins/echarts/echarts.min.js"></script>

<!-- 自定义js -->
<script src="/js/content.js?v=1.0.0"></script>


<!-- ECharts demo data -->
<!--<script src="/js/demo/echarts-demo.js"></script>-->
<script>
    var myChart;
    var DeviceInfo;
    var MaxMessageId=0;
    var data=[];
    var list;

    function checknew() {
        $.post('/devicemessages/getlatest',{DeviceId:GetUrlParam("Id"),Id:MaxMessageId},function(datalist) {
            datalist1=datalist;
            console.log(datalist.length);
            if (datalist.length>0){
                MaxMessageId=datalist[0].Id
                for (var i=datalist.length-1;i>=0;i--){
                    data.shift();
                    data.push([datalist[i].DeviceMessagesTime.replace("T"," ").replace("+08:00",""),datalist[i].Data]);
                }
            }
        })
    }

    function setChart(){
                myChart.setOption({
                    xAxis: {
                        data: data.map(function (item) {
                            return item[0];
                        })
                    },
                    series: [{
                        data: data.map(function (item) {
                            return item[1];
                        })
                    }]
                });
    }
    setInterval('checknew()',5000);
    setInterval('setChart()',1000);
    var lastNewDate=0;

    $.post('/devicemessages/getlist',{DeviceId:GetUrlParam("Id")},function(recvData){
        list=recvData;
        dataList=recvData.MessageList;
        MaxMessageId=dataList[0].Id
        DeviceInfo=recvData.Device;
        for (var i=dataList.length-1;i>=0;i--){
            data.push([dataList[i].DeviceMessagesTime.replace("T"," ").replace("+08:00",""),dataList[i].Data]);
        }
        createChart('echarts-bar-chart','温度 ℃','温度');

    })

    function createChart(chartid,text,name) {
        myChart= echarts.init(document.getElementById(chartid));
        myChart.setOption(option = {
            tooltip: {
                trigger: 'axis'
            },
            xAxis: {
                data: data.map(function (item) {
                    return item[0];
                })
            },
            yAxis: {
                splitLine: {
                    show: false
                },
                name: text,
                nameLocation:'end'

            },
            visualMap: {
                top: 10,
                right: 10,
                pieces: [{
                    gt: -20,
                    lte: 15,
                    color: '#096'
                }, {
                    gt: 15,
                    lte: 30,
                    color: '#63B8FF'
                }, {
                    gt: 30,
                    lte: 45,
                    color: '#FF7F24'
                }, {
                    gt: 45,
                    lte: 200,
                    color: '#FF0000'
                }],
                outOfRange: {
                    color: '#FF0000'
                }
            },
            series: [{
                name: name,
                type: 'line',
                lineStyle:{
                    normal:{
                        width:2,  //连线粗细
                    }
                },
                markLine: {
                    silent: true,
                    data: [{
                        yAxis: 30
                    }, {
                        yAxis: 45
                    }]
                },
                smooth:false,
                data: data.map(function (item) {
                    return item[1];
                })}
            ]

        });
    }

    function todate(fmt,UNIX_Timestamp) {
        var date = new Date(UNIX_Timestamp * 1000);
        console.log(date)
        return date.Format(fmt);
    }

    function GetUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]);
        return null; //返回参数值
    }
</script>
</body>

</html>
